package com.example.tms.controller;

import com.example.tms.common.annotation.LogAction;
import com.example.tms.common.enums.Result;
import com.example.tms.entity.Project;
import com.example.tms.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/projects")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    // GET 请求：获取所有项目
    @LogAction("查询所有项目")
    @GetMapping("")
    public Result selectAll(Project project) {
        List<Project> list = projectService.selectAll();
        return Result.success(list);
    }
    
    // GET: 根据 ID 获取项目
    @LogAction("根据id查询")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Project project = projectService.selectById(id);
        return Result.success(project);
    }

    // POST: 添加项目
    @LogAction("添加项目")
    @PostMapping("")
    public Result add(@RequestBody Project project) {
        int result = projectService.insert(project);
        return result > 0 ? Result.success() : Result.error("添加失败");
    }

    // PUT: 更新项目信息
    @LogAction("更新项目信息")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Project project) {
        project.setId(id); // 将路径参数的 id 设置到 project 对象中
        int result = projectService.update(project);
        return result > 0 ? Result.success() : Result.error("更新失败");
    }

    // DELETE: 删除项目
    @LogAction(value = "删除项目",level = "WARN")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        int result = projectService.deleteById(id);
        return result > 0 ? Result.success() : Result.error("删除失败");
    }
}
